package JAVA300.第十二章网络编程;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * tcp
 * 服务器存储文件
 *

 */

public class web05_TCP4_FileServer {
    public static void main(String[] args) throws IOException {
        System.out.println("--------Server-----------");
        //1。指定端口 使用ServerSocket创建服务器
        ServerSocket server = new ServerSocket(8888);

        //2.阻塞式的等待链接 accept
        Socket socket = server.accept();
        System.out.println("一个客户端建立了连接");
        //3。操作：输入输出流 **************文件存储
        InputStream is = new BufferedInputStream(socket.getInputStream());
        OutputStream os = new BufferedOutputStream(new FileOutputStream("testpic_TCP.png"));

        byte[] flush = new byte[1024];
        int len = -1;
        while ((len = is.read(flush))!= -1){
            os.write(flush,0,len);
        }
        os.flush();

        os.close();
        is.close();
        socket.close();


    }
}
